package com.woniu.microworkorderserver.workgroupmag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.WorkGroupInfoEntity;
import com.woniu.microcommonsserver.entity.WorkgroupUserInfoEntity;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microcommonsserver.param.PageResult;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microworkorderserver.util.JsonToObject;
import com.woniu.microworkorderserver.workgroupmag.service.WorkGroupService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/workGroup")
public class WorkGroupController {

    @Resource
    private WorkGroupService workGroupServiceImpl;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg save(@RequestBody JSONObject jsonObject){
        rabbitTemplate.convertAndSend("msg.exchange","msg.info","新增工作组");
        List<Integer> userIds = JsonToObject.getWorkUserIds(jsonObject);
        WorkGroupInfoEntity entity = JsonToObject.getWorkGroups(jsonObject).get(0);
        workGroupServiceImpl.save(entity,userIds);
        return ResponseMsg.builder().code(200).msg("保存成功").build();
    }

    @GetMapping(value = "/page",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findPage(PageRequest page, WorkGroupInfoEntity entity){
        PageInfo pageInfo = workGroupServiceImpl.findPage(page,entity);
        PageResult<WorkGroupInfoEntity> result = new PageResult<>();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return ResponseMsg.builder().code(200).msg("查询成功").data(result).build();
    }

    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg delete(@RequestParam("workGroupId") long workGroupId){
        System.out.println(workGroupId);
        workGroupServiceImpl.delete(workGroupId);
        return ResponseMsg.builder().code(200).msg("删除成功").build();
    }

    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg update(@RequestBody JSONObject jsonObject){
        List<Integer> userIds = JsonToObject.getWorkUserIds(jsonObject);
        WorkGroupInfoEntity entity = JsonToObject.getWorkGroups(jsonObject).get(0);
        System.out.println(userIds);
        System.out.println(entity);
       workGroupServiceImpl.update(entity,userIds);
        return ResponseMsg.builder().code(200).msg("修改成功").build();
    }
}
